<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<%@ include file="/Views/Common/header.jsp"%>
<!-- 页面特殊加载的js -->
<script type="text/javascript"
	src="${ctx}/Resource/js/grid.locale-cn.js"></script>
<script type="text/javascript"
	src="${ctx}/Resource/js/jquery.jqGrid.src.js"></script>
<script type="text/javascript"
	src="${ctx}/Resource/js/jquery.jqGrid.min.js"></script>
</head>
<body>
    <%@ include file="/Views/Common/top.jsp"%>
	<div class="container-fluid">
		<div class="row">
			<%@ include file="/Views/Common/navbar.jsp"%>
			<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
				<h4 class="page-header">
					<i class="glyphicon glyphicon-home" style="padding: 10px"></i>${title}
				</h4>
				<div class="container-fluid"
					style="border-radius: 5px; background: #f4f4f4; position: relative; margin: 10px, 10px, 10px, 10px; padding: 20px">
					<form id="search">
						<s:iterator value="htmlObjs" id="obj">
					        ${obj}
					    </s:iterator>
					</form>
				</div>
				<div class="right">
					<button type="button" class="btn btn-default">帮助</button>
					<button type="button" onclick="javascript:window.location.href='product_add'" class="btn btn-default">新建</button>
					<button type="button" id="update" class="btn btn-default">编辑</button>
					<button type="button" id="delete" class="btn btn-default">删除</button>
					<button type="button" id="query" class="btn btn-info">查询</button>
				</div>
				<div style="clear:both"></div>
				<table id="grid-table"></table>
				<div id="grid-pager"></div>
			</div>
		</div>
	</div>
	<script type="text/javascript">
		var models = ${colModel};
        var names = ${colName};
        
		jQuery(function($) {
			var grid_selector = "#grid-table";
			var pager_selector = "#grid-pager";

			jQuery(grid_selector).jqGrid(
					{
						url:"product_getProduct",
						datatype : "json",
						colNames : names,
						colModel : models,
						viewrecords : true,
						rowNum : 10,
						rowList : [ 10, 20, 30 ],
						pager : pager_selector,
						altRows : true,
						multiselect : true,
						multiboxonly : true,

						loadComplete : function(data) {
							if(data.success==false)
								alert("获取数据失败:"+data.info);
							setTimeout(function() {
								doResize();
								//统一三个部分的宽度
								$(".ui-jqgrid-htable").attr("style",$(pager_selector).attr("style"));
								$(grid_selector).attr("style",$(pager_selector).attr("style"));
							}, 0);
						},
						shrinkToFit : true,
						autowidth : true,
						grouping : false,
						sortname : "product_id",
						sortorder : "ASC",
						groupingView : {
							groupField : [ 'product_name' ],
							groupColumnShow : [ true ],
							groupText : [ '<b>{0}共有{1}条记录</b>' ],
							groupOrder : [ 'desc' ]
						},
					});
			//navButtons
			jQuery(grid_selector).jqGrid('navGrid', pager_selector, { //navbar options
				edit : false,
				add : false,
				del : false,
				search : false,
				refresh : false,
				view : false,
			})
		});
		
		$("#query").click(function() {
			var sdata = $("#search").serializeArray();
			var postData = $("#grid-table").jqGrid("getGridParam", "postData");
			
			for(var i = 0;i<sdata.length;i++)
		    {
				postData[sdata[i]['name']]=sdata[i]['value'];
		    }
			$("#grid-table").jqGrid("setGridParam", {
				datatype : "json",
				search : true,
				mtype : "post"
			}).trigger("reloadGrid");

		});
		
		$("#delete").click(function() {
			var selectedIds = $("#grid-table").jqGrid("getGridParam", "selarrrow");
			if (selectedIds.length == 0)
				alert("请至少选择一行数据");
			else {
				var obj = new Array();
				var length = selectedIds.length;
				for ( var i = 0; i < length; i++) {
					obj[i] = $("#grid-table").jqGrid("getRowData", selectedIds[i]).product_id;
				}
//				if (confirm("确定要删除吗?删除后将无法恢复")) {
					$.ajax({
						type : "POST",
						url : "product_delete",
						data : "delete_value=" + JSON.stringify(obj),
						dataType : 'json',
     					error : function(data) {
							alert("删除失败");
						},
						success : function(data) {
							if(data.success==true)
								{
								alert("删除成功");
								}else{
									alert("删除失败" + data.info);
								}
						},
						complete:function(){
							$("#grid-table").trigger("reloadGrid");
						}
     				});
//				}
			}
		})
		
		$("#update").click(function() {
			var selectedIds = $("#grid-table").jqGrid("getGridParam", "selarrrow");
			if (selectedIds.length==0){
				alert("请选择数据");
			}else if (selectedIds.length>1){
				alert("一次只能编辑一行数据");
			}else{
				var length = selectedIds.length;
				var obj = $("#grid-table").jqGrid("getRowData", selectedIds[0]).product_id;
				window.location.href="product_update?id="+obj;
			}
		})
	</script>
</body>
</html>